package com.retail.bbq.cola.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.retail.bbq.cola.entity.ColaQuestionBank;
import com.retail.bbq.cola.service.ColaQuestionBankService;
import com.retail.bbq.cola.mapper.ColaQuestionBankMapper;
import com.retail.bbq.cola.service.QuestionCommonCacheService;
import com.retail.bbq.cola.vo.request.ColeQuestionRequest;
import com.retail.bbq.cola.vo.response.ColaQuestionDetailVo;
import com.retail.bbq.cola.vo.response.QuestionOptionsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author jarvis.zhang
 * @description 针对表【cola_question_bank(可乐题库表)】的数据库操作Service实现
 * @createDate 2025-06-17 13:41:24
 */
@Slf4j
@Service
public class ColaQuestionBankServiceImpl extends ServiceImpl<ColaQuestionBankMapper, ColaQuestionBank> implements ColaQuestionBankService {

    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private QuestionCommonCacheService questionCommonCacheService;

    @Override
    public ColaQuestionDetailVo queryQuestionDetailWithUserInfo(ColeQuestionRequest request) {
        // 1.封装出参
        ColaQuestionDetailVo colaQuestionDetailVo = new ColaQuestionDetailVo();
        // 2.查询题目
        ColaQuestionBank colaQuestionBank = questionCommonCacheService.queryColaQuestionBank(request.getBuCode(), request.getSetupId());
        if (ObjectUtil.isNotEmpty(colaQuestionBank)) {
            try {
                BeanUtil.copyProperties(colaQuestionBank, colaQuestionDetailVo);
                List<QuestionOptionsVo> optionsList = JSONUtil.toList(colaQuestionBank.getOptions(), QuestionOptionsVo.class);
                colaQuestionDetailVo.setOptionsList(optionsList);
                colaQuestionDetailVo.setQuestionId(colaQuestionBank.getId());
            } catch (Exception e) {
                log.error("每周题目封装异常", e);
            }
        }
        // 3.封装当前人员答题记录
        Boolean isPass = questionCommonCacheService.queryCustomerAnswerResult(colaQuestionDetailVo.getQuestionId(), request.getOpenId());
        colaQuestionDetailVo.setHavePass(isPass);
        return colaQuestionDetailVo;
    }
}




